<template>
	<div class="category-list">
		<list-title :data="header"></list-title>
		<list-table :list="appInfoList" :tableLoading="tableLoading" :footer="false" :sort="true" @setSort="setSort">
			<template v-slot:data>
				<el-table-column prop="app_name" label="应用名称"></el-table-column>
				<el-table-column prop="app_cat_id" label="应用分类"></el-table-column>
				<el-table-column prop="icon" label="Icon">
					<template slot-scope="scope">
						<span :class="'icon iconfont icon-' + scope.row.icon"></span>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="app_url" label="域"></el-table-column> -->
				<el-table-column prop="outside" label="外链">
					<template slot-scope="scope">
						<span :class="'el-icon-' + ((scope.row.outside == 1)?'close':'check')"></span>
					</template>
				</el-table-column>
				<el-table-column label="显示状态">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.display" @change="changeDisplay(scope.$index, scope.row)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200">
					<template slot-scope="scope">
						<el-button type='text' size="mini" @click="setMenu(scope.row.id, scope.row.app_name)" v-if="auth.menu">菜单</el-button>
						<el-button type='text' size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="auth.editor">编辑</el-button>
						<el-popover
						placement="top"
						width="160"
						v-model="delTip[scope.$index+1]" v-if="auth.delete">
							<p class="del-tip"><span class="el-icon-question"></span>确定删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="delTip.splice(scope.$index+1, 1, false)">取消</el-button>
								<el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference" :disabled='submitLock[scope.$index+1]'>
								删除 <span class="el-icon-loading" v-if='submitLock[scope.$index+1]'></span>
							</el-button>
						</el-popover>
					</template>
				</el-table-column>
			</template>
		</list-table>
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import ListTable from "pc/common/ListTable"
import {changeToBool, getSortData, judgeAuth} from "static/js/common.js"
import {domain} from '@/api';
import {mapGetters} from 'vuex'

export default {
	inject: ['reload'],
	data() {
		return {
			/*头部数据*/
			header: {
				title: '应用列表',
				reload: true,
				buttons: [
					{
						title: '添加应用',
						type: 'primary',
						pathName: 'pc/devper/app/app-info/AppInfo',
						auth: false,
					}
				]
			},
			auth: {
				menu: false,//菜单
				editor: false,//编辑
				delete: false,//删除
				changeState: false,//显示状态
				sort: false,//排序
			},
			/*列表数据*/
			appInfoList: [
				// {
				// 	id: 1,
				// 	app_name: '应用名称',
				// 	describe: '描述',
				// 	app_cat_id: 1,
				// 	icon: 'panel',
				// 	app_url: 'panel',
				// 	preview: [],
				// 	outside: 'panel',
				// 	display: false,
				// },
			],
			tableLoading: true,
			delTip: [],//刪除提示
			submitLock: [],
		};
	},
	components: {
		ListTitle,
		ListTable
	},
	created: function() {
		/*获取列表数据*/
		this.getAppInfoList()
		/*权限判断显示*/
		this.header.buttons[0].auth = (judgeAuth('addAppInfo', this.allMenuList) == undefined)?false:true
		this.auth.menu = (judgeAuth('menuList', this.allMenuList) == undefined)?false:true
		this.auth.editor = (judgeAuth('modifyAppinfo', this.allMenuList) == undefined)?false:true
		this.auth.delete = (judgeAuth('deleteAppinfo', this.allMenuList) == undefined)?false:true
		this.auth.changeState = (judgeAuth('setAppInfoDisplay', this.allMenuList) == undefined)?false:true
		this.auth.sort = (judgeAuth('appInfoSort', this.allMenuList) == undefined)?false:true
	},
	methods: {
		/*获取列表数据*/
		getAppInfoList: function(){
			this.$baseURL(domain.ac)
			if(this.$api.appInfoList != undefined){
				this.$get(this.$api.appInfoList).then(res => {
					// console.log(res);
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code === 0){
						if(res.data.data != undefined){
							this.appInfoList = []
							Object.keys(res.data.data).forEach((k) => {
								this.appInfoList.push ({
									id: res.data.data[k].app_info_id,
									app_name: res.data.data[k].app_name,
									app_cat_id: (res.data.data[k].category != null)?res.data.data[k].category.cat_name:'',
									icon: res.data.data[k].icon,
									app_url: res.data.data[k].app_url,
									outside: res.data.data[k].outside,
									display: changeToBool(res.data.data[k].display)
								});
							})
						}
						this.tableLoading = false
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*编辑*/
		handleEdit(index, row) {
			this.$router.push(
				{
					name: 'pc/devper/app/app-info/AppInfo', 
					query: {
						id: row.id
					}
				}
			)
		},
		/*删除应用*/
		handleDelete(index, row) {
			if(!this.submitLock[index+1]){
				this.submitLock[index+1] = true//锁住当前的 删除按钮
				this.delTip.splice(index+1, 1, false)//提示消失
				/*请求后台删除*/
				this.$baseURL(domain.ac)
				if(this.$api.deleteAppinfo != undefined){
					this.$get(this.$api.deleteAppinfo, {
						'id': row.id
					}).then(res => {
						// if(res.data.code == 10007){//后台提示需要登录
						// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
						// 	this.reload();
						// }
						if(res.data.code == 0){
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'success'
							});
							this.appInfoList.splice(index, 1);//删除当前行
						}else{
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'error'
							});
						}
						this.submitLock[index+1] = false//释放当前的 删除按钮
					})
					.catch(err => {})
					.finally(() => {}) 
				}
			}
		},
		/* 设置菜单 */
		setMenu(app_info_id, app_name){
			/*这里的路由，如果后台和程序改了路径，这些需要一起改*/
			this.$router.push({
				name: 'pc/devper/menu/MenuList',
				query: {
					app_info_id: app_info_id,
					app_name: app_name
				} 
			})
		},
		/*列表中直接修改 是否显示*/
		changeDisplay(index, row){
			/*请求后台*/
			this.$baseURL(domain.ac)
			if(this.$api.setAppInfoDisplay != undefined){
				this.$post(this.$api.setAppInfoDisplay, {
					'AppInfo': {
						'id': row.id,
						'display': row.display
					}
				}).then(res => {
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*排序*/
		setSort(newIndex, oldIndex){
			// console.log(newIndex, oldIndex)
			this.appInfoList = JSON.parse(JSON.stringify(this.appInfoList))
			let oldVal = this.appInfoList.splice(oldIndex, 1)[0]
			this.appInfoList.splice(newIndex, 0, oldVal)
			// console.log(this.appInfoList)
			let sort = getSortData(this.appInfoList)

			this.$baseURL(domain.ac)
			if(this.$api.appInfoSort != undefined){
				this.$post(this.$api.appInfoSort, {
					'sort': sort
				}).then(res => {
					// if(res.data.code == 10007){//后台提示需要登录
					// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
					// 	this.reload();
					// }
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		}
	},
	computed: {
		...mapGetters([
			'allMenuList'
		])
	}
};
</script>

<style lang="stylus" scoped>
.el-table__row
	td:last-child
		span
			margin-left: 10px
</style>